<?php

class User_model extends CI_Model {

    /**
     * Constructor
     */
    function __construct() {
        parent::__construct();
    }

    function _loadAll() {
        $ress = $this->db->query("select a.*, b.role from m_user a left join m_role b ON(a.id_role = b.id) order by a.username ASC")->result_array();
        return $ress;
    }

   function _loadRole() {
        $ress = $this->db->query("select * from m_role")->result_array();
        return $ress;
    }
    
    function _getByUsername($username){
        $ress = $this->db->query("select a.*, b.role from m_user a left join m_role b ON(a.id_role = b.id) where a.username = '$username'")->row_array();
        return $ress;
    }
    
    

    function uploadImage($file_name) {
        
        $config = array();
        $data = array();
        
        $config['upload_path'] = UPLOAD_PATH_USER;
        $config['allowed_types'] = 'gif|png|jpg|jpeg';
        $config['file_name'] = $file_name;
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);

        $data_upload = $_FILES["path_image"]['name'];
        $tipe_file = $_FILES["path_image"]['type'];
        $this->upload->initialize($config); 
        
        if (empty($data_upload)) {
            $data['path_image'] = '';
        } else {

                if (!$this->upload->do_upload('path_image')) {
                    $data['path_image'] = '';
                } else {
                    $upload_data = $this->upload->data();
                    $data['path_image'] = $upload_data['file_name'];
                }
           
        }
        
        unset($config);
        
        return $data['path_image'];
    }
    
    
    function _checkUsername($username) {
       $ress = $this->db->query("select username from m_user where username = '$username'")->row_array();
       if(empty($ress['username'])) { return TRUE;  } 
       else { return FALSE;  } 
    }
    
    function _checkEmail($email) {
       $ress = $this->db->query("select email from m_user where email = '$email'")->row_array();
       if(empty($ress['email'])) { return TRUE;  } 
       else { return FALSE;  } 
    }
    
  
}

